<?php
class Controller_User extends Controller_Template_Website
{
    public function action_profile()
    {
        $userAuth = Auth::instance()->get_user();
        $userModel = new Model_User();
        $userInfos = $userModel->getUser($userAuth->id);
        $userInfos = $userInfos->as_array();

        $imageModel = new Model_Image();
        $userInfos['countImages'] = $imageModel->countImagesFromUser($userAuth->id);

        $this->template->title = "User Profile";
        $this->template->content = View::factory('user/profile')->set('user',$userInfos);
    }

    public function action_change()
    {
        if ($this->request->method() == Request::POST) {

            $post = $this->request->post();

            $validation = Validation::factory($post);
            $validation
                ->rule(True, 'not_empty') // fuer alle Post Werte
                ->rule('passwordOld', 'min_length', array(':value', '3'))
                ->rule('passwordNew', 'min_length', array(':value', '3'));

            if ($validation->check()) {

                $user     = new Model_User();
                $userAuth = Auth::instance()->get_user();
                // return ORM User Model
                $user = $user->getUser($userAuth->id);

                $oldPw = Auth::instance()->hash($post['passwordOld']);
                $newPw = Auth::instance()->hash($post['passwordNew']);

                if ($user->password == $oldPw) {

                    $user->password = $newPw;

                    if ($user->check()) {
                        $user->save();
                        Message::set( Message::SUCCESS, 'Password change successful' );
                        $this->redirect('user/change');
                    }

                } else {
                    Message::set( Message::ERROR, 'Old password is incorrect' );
                    $this->redirect('user/change');
                }

            } else {
                Message::set( Message::ERROR, $validation->errors('userMessages') );
                $this->redirect('user/change');
            }
        }


        $this->template->title = "Change Password";
        $this->template->content = View::factory('user/change');
    }

    public function action_manage()
    {
        $userAuth   = Auth::instance()->get_user();
        $imageModel = new Model_Image();
        $count = $imageModel->countAllImagesFormUser($userAuth->id);

        $pagination = Pagination::factory(array(
                'total_items' => $count,
                'items_per_page' => 10,
            )
        );

        $userImages = $imageModel->getAllImagesFromUser($userAuth->id, $pagination->offset, $pagination->items_per_page);

        $pagination->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action()));


        $this->template->title = "Manage Images";
        $this->template->content = View::factory('user/manage')->set('userImages', $userImages)->set('pagination', $pagination);
    }
}